package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.MusicDao;
import com.jbp.model.Music;
import com.jbp.service.MusicService;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import java.util.Map;

/**
 * (Music)表服务实现类
 * @author cc
 * @since 2020-10-14 10:42:04
 */
@Service
public class MusicServiceImpl extends ServiceImpl<MusicDao, Music> implements MusicService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Music> queryWrapper = new QueryWrapper<>();
        if (params.get("title") != null){
            queryWrapper.like("title",params.get("title")).or().like("singer",params.get("title"));
        }
        queryWrapper.orderByDesc("hot");
        IPage<Music> page = this.page(
                new Query<Music>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void like(Integer musicId) {
        Music music = getById(musicId);
        if (music!=null) {
            music.setHot(music.getHot()+1);
            updateById(music);
        }
    }

    @Override
    public PageUtils myMusics(Map<String, Object> params) {
        QueryWrapper<Music> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",params.get("userId"));
        queryWrapper.orderByDesc("hot");
        IPage<Music> page = this.page(
                new Query<Music>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }
}